package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Newsdetails;
import com.ysd.util.DBManager;

public class SuiJiXinWenDaoImpl {
	public List<Newsdetails> getNewsdetails() {
		List<Newsdetails> list =new ArrayList<Newsdetails>();
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Newsdetails newsdetails = null;
		try {// 正常
				// 2.创建Statement对象
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * from newsdetails order by rand() limit 1;";
			// 4.执行sql语句
			rs = st.executeQuery(sql);// execute执行Query查询
			// 5.遍历结果集
			while (rs.next()) {
				int newsId = rs.getInt("newsId");
				String newsTitle = rs.getString("newsTitle");
				String nwesContent = rs.getString("nwesContent");
				int newsCategoryId = rs.getInt("newsCategoryId");
				newsdetails = new Newsdetails(newsId, newsTitle, nwesContent, newsCategoryId);
				list.add(newsdetails);
				
			}
			return list;// 正常执行返回用户集合
		} catch (SQLException e) {// 异常
			e.printStackTrace();
			return null;// 异常执行返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
}
